package com.yk.faceapplication.http;

import java.util.Calendar;
import java.util.Map;
import java.util.TimeZone;

import org.json.JSONObject;


import android.content.Context;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.yk.faceapplication.MyApplication;
import com.yk.faceapplication.callback.JsonRequestCb;
import com.yk.faceapplication.callback.NormalJsonRequestCb;
import com.yk.faceapplication.entity.ResponseObj;
import com.yk.faceapplication.util.LogDebugger;
import com.yk.faceapplication.util.SignUtils;

public class JsonRequest {
	public static void request(Map<String, String> form, final String url, final Context ctx, final JsonRequestCb cb) {
		// form.put("sysTime", Calendar.getInstance().getTimeInMillis()-1000*60*60*8 + "");
		Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
		form.put("sysTime", c.getTimeInMillis() + "");
		String sign = SignUtils.sign(form, ((MyApplication) ctx.getApplicationContext()).getSecretKey());
		form.put("sign", sign);
		LogDebugger.info(form + "");
		VolleyUtil jsonRequest = new VolleyUtil(url, new Response.Listener<JSONObject>() {
			@Override
			public void onResponse(JSONObject response) {
				LogDebugger.info(url + ":" + response);
				ResponseObj rObj = JSON.parseObject(response.toString(), ResponseObj.class);
				if (rObj.getState() == 1) {
					cb.doJsonObject(rObj);
				}
				else {
					Toast.makeText(ctx, rObj.getErrMsg(), Toast.LENGTH_LONG).show();
				}
			}
		}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError arg0) {
				arg0.printStackTrace();
				if (System.currentTimeMillis() - ((MyApplication) ctx.getApplicationContext()).getShowWrongMsgTime() > 2000) {
					Toast.makeText(ctx, "没有网络连接", Toast.LENGTH_LONG).show();
					((MyApplication) ctx.getApplicationContext()).setShowWrongMsgTime(System.currentTimeMillis());
				}
				
				cb.fail();
			}
		}, form);
		((MyApplication) ctx.getApplicationContext()).getrQueue().add(jsonRequest);
	}
	
	public static void requestNoParameter(final String url, final Context ctx, final NormalJsonRequestCb cb, Map<String, String> form) {
		LogDebugger.info("url:" + url);
		LogDebugger.info("form:" + form);
		VolleyUtil jsonRequest = new VolleyUtil(url, new Response.Listener<JSONObject>() {
			@Override
			public void onResponse(JSONObject response) {
				LogDebugger.info(url + ":" + response);
				cb.doJsonObject(response);
			}
		}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError arg0) {
				arg0.printStackTrace();
				LogDebugger.info("arg0:" + arg0.getMessage());
				if (System.currentTimeMillis() - ((MyApplication) ctx.getApplicationContext()).getShowWrongMsgTime() > 2000) {
//					Toast.makeText(ctx, "没有网络连接", Toast.LENGTH_LONG).show();
					((MyApplication) ctx.getApplicationContext()).setShowWrongMsgTime(System.currentTimeMillis());
				}
				cb.fail();
			}
		}, form);
		((MyApplication) ctx.getApplicationContext()).getrQueue().add(jsonRequest);
	}
}
